package edu.zzuli.model.xt.jqqx.ywjsgl.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.zzuli.common.Pagination;
import edu.zzuli.common.TreeNode;
import edu.zzuli.model.dao.BaseDaoEntity;
import edu.zzuli.model.dao.BasePO;
import edu.zzuli.model.orm.factory.DaoFactory;
import edu.zzuli.model.orm.po.GG_CZLB;
import edu.zzuli.model.orm.po.GG_LBGN;
import edu.zzuli.model.orm.po.GG_XTGN;
import edu.zzuli.model.xt.jqqx.ywjsgl.iface.YwjsglService;

/**
 * @author tianshaojie
 * @date 2011-1-10
 * @discription : 
 */
@Transactional
@Service("ywjsglService")
public class YwjsglServiceImpl implements YwjsglService{
	
	@Resource
	private BaseDaoEntity baseDaoEntity;
	
	@Resource
	private DaoFactory daoFactory;
	
	public List getGG_XTGN() {
		return this.daoFactory.getGG_XTGNDao().getGG_XTGNList();
	}
	
	public List getGG_CZLB() {
		return this.daoFactory.getGG_CZLBDao().getGG_CZLBList();
	}
	
	public List selectSplit(String sql, Pagination pagination) {
		return this.baseDaoEntity.selectSplit(sql, pagination);
	}
	
	public List selectSplit(String sql, SqlParameterSource parameterSource, Pagination pagination) {
		return this.baseDaoEntity.selectSplit(sql, parameterSource, pagination);
	}
	
	public void saveOrUpdatePO(BasePO basePO) {
		if (basePO.getStrKeyWhere() != null && basePO.getStrKeyWhere().length() > 0) {
			this.baseDaoEntity.update(basePO);
		} else {
			this.baseDaoEntity.save(basePO);
		}
	}
	
	public BasePO selectSinglePO(BasePO basePO) {
		return this.baseDaoEntity.selectSingle(basePO, basePO.getStrKeyWhere());
	}
	
	public void deleteGG_CZLB(String ids) {
		if (ids != null && !"".equals(ids)) {
			String[] idsArr = ids.split(",");
			for (String lbid : idsArr) {
				this.baseDaoEntity.getHibernateDao().delete(new GG_LBGN(),new StringBuffer(" lbid = '" + lbid + "'"));
				this.baseDaoEntity.getHibernateDao().delete(new GG_CZLB(lbid));
			}
		}
	}
	
	public int getIntBySql(String sql) {
		return this.baseDaoEntity.getJdbcTemplate().queryForInt(sql);
	}
	
	public List<TreeNode> getXtgnTreeNode(String czlbId) {
		String czlbSql = "select gnid from GG_LBGN where lbid = :LBID";
		Map paramMap = new HashMap();
		paramMap.put("LBID", czlbId);
		List<Map<String, Object>> gnidList = this.baseDaoEntity.queryForList(czlbSql, paramMap);
		
		String sql = "select gn.id, gn.fid, gn.jdmc, gn.jdlb, SYS_CONNECT_BY_PATH(to_char(gn.sxh, '0000'), '.') sort\n"
			+ " from gg_xtgn  gn\n"
			+ " start with gn.fid is null\n"
			+ " connect by gn.fid = prior gn.id\n"
			+ " order by sort";
		List<Map<String, Object>> listMap = this.baseDaoEntity.getJdbcTemplate().queryForList(sql);
		List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
		for(Map map:listMap) {
			String id = (String)map.get("ID");
			TreeNode node = new TreeNode(id,(String)map.get("JDMC"),(String)map.get("FID"));
			for(Map mapXtgn:gnidList) {
				if (id.equals((String)mapXtgn.get("GNID"))) {
					node.setChecked(true);
					break;
				}
			}
			listTreeNode.add(node);
		}
		return listTreeNode;
	}
	
	public void deleteBasePO(BasePO basePO) {
		this.baseDaoEntity.getHibernateDao().delete(basePO, basePO.getStrKeyWhere());
	}
	
	public void saveGnqx(String czlbId,String xtgns) {
		this.baseDaoEntity.getHibernateDao().delete(new GG_LBGN(), new StringBuffer(" LBID = '" + czlbId + "'"));
		String[] xtgnArr = xtgns.split(",");
		List gg_lbgnList = new ArrayList();
		for (String xtgn : xtgnArr) {
			GG_LBGN gg_lbgn = new GG_LBGN();
			gg_lbgn.setGG_CZLB(new GG_CZLB(czlbId));
			gg_lbgn.setGG_XTGN(new GG_XTGN(xtgn));
			gg_lbgnList.add(gg_lbgn);
		}
		if (gg_lbgnList.size() > 0) {
			this.baseDaoEntity.getHibernateDao().save(gg_lbgnList);
		}
	}
}
